package com.beijing.demo.queue;

/**
 * 曹旭
 * 2025/5/7
 * 实现循环双端队列
 * https://leetcode.cn/problems/design-circular-deque/
 */
public class MyCircularDeque {
    public int[] array;
    public int size;
    public int l;
    public int r;
    public int limit;
    public MyCircularDeque(int k) {
        array = new int[k];
        l = r = 0;
        size = 0;
        limit = k;
    }

    public boolean insertFront(int value) {
        if(isFull()){
            return false;
        }
        if(size == 0){
            array[0] = value;
            l = r = 0;
        } else {
            l = l == 0 ? limit - 1 : --l;
            array[l] = value;
        }
        size++;
        return true;
    }

    public boolean insertLast(int value) {
        if(isFull()){
            return false;
        }
        if(size == 0){
            array[0] = value;
            l = r = 0;
        } else {
            r = r == limit - 1 ? 0 : ++r;
            array[r] = value;
        }
        size++;
        return true;
    }

    public boolean deleteFront() {
        if(isEmpty()){
            return false;
        }
        l = l == limit - 1 ? 0 : ++l;
        size--;
        return true;
    }

    public boolean deleteLast() {
        if(isEmpty()){
            return false;
        }
        r = r == 0 ? limit - 1 : --r;
        size--;
        return true;
    }

    public int getFront() {
        if(isEmpty()){
            return -1;
        }
        return array[l];
    }

    public int getRear() {
        if(isEmpty()){
            return -1;
        }
        return array[r];
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public boolean isFull() {
        return size == limit;
    }

    public static void main(String[] args) {
        MyCircularDeque deque = new MyCircularDeque(2);
//        ["MyCircularDeque","insertFront","deleteLast","getFront",  "insertLast 5 ","insertFront 0 ",  "getFront", "getRear" ,"getFront","getFront", "getRear" ,"insertLast"]
//[[2],[7],[],[],[5],[0],[],[],[],[],[],[0]]
        deque.insertFront(7);
        deque.deleteLast();
        deque.insertLast(5);
        deque.insertFront(0);
        deque.getRear();
    }

}




/*class MyCircularDeque {
    public int[] array;
    public int size;
    public int limit;
    public int l;
    public int r;

    public MyCircularDeque(int k) {
        array = new int[k];
        size = 0;
        limit = k;
        l = r = 0;
    }

    public boolean insertFront(int value) {
        if(isFull()){
            return false;
        }else {
            if(size == 0){
                array[0] = value;
                l = r = 0;
            }else {
                l = l == 0 ? limit - 1 : --l;
                array[l] = value;
            }
            size++;
            return true;
        }
    }

    public boolean insertLast(int value) {
        if(isFull()){
            return false;
        } else {
            if(size == 0){
                array[0] = value;
                l = r = 0;
            } else {
                r = r == limit - 1 ? 0 : ++r;
                array[r] = value;

            }
            size++;
            return true;
        }
    }

    public boolean deleteFront() {
        if(isEmpty()){
            return false;
        }
        l = l == limit - 1 ? 0 : ++l;
        size--;
        return true;
    }

    public boolean deleteLast() {
        if(isEmpty()){
            return false;
        }
        r = r == 0 ? limit - 1 : --r;
        size--;
        return true;
    }

    public int getFront() {
        if(isEmpty()){
            return -1;
        }
        return array[l];
    }

    public int getRear() {
        if(isEmpty()){
            return -1;
        }
        return array[r];
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public boolean isFull() {
        return size == limit;
    }
}

*//**
 * Your MyCircularDeque object will be instantiated and called as such:
 * MyCircularDeque obj = new MyCircularDeque(k);
 * boolean param_1 = obj.insertFront(value);
 * boolean param_2 = obj.insertLast(value);
 * boolean param_3 = obj.deleteFront();
 * boolean param_4 = obj.deleteLast();
 * int param_5 = obj.getFront();
 * int param_6 = obj.getRear();
 * boolean param_7 = obj.isEmpty();
 * boolean param_8 = obj.isFull();
 */
